Hands-On Exercise 3 (Programming Interactive Data Visualisation with R, Programming Animated Statistical Graphics with R)
Author
Zanelle Lee Wenfang
Modified
January 31, 2025
pacman::p_load(ggiraph, plotly, patchwork, DT, tidyverse)exam_data <-read_csv("Data files/Exam_data.csv")#ONE: Tooltip#creating interactive version of ggplot2 geom (ggplot object) using#geom_dotplot_interactive()p <-ggplot(data=exam_data, aes(x = MATHS)) +geom_dotplot_interactive(aes(tooltip = ID),stackgroups =TRUE, binwidth =1, method ="histodot") +scale_y_continuous(NULL, breaks =NULL)#using girafe() of ggiraph to make it a svg object displayed on html page#hovering over data displays student IDgirafe(ggobj = p,width_svg =6,height_svg =6*0.618)
#creating new field called tooltip and also populating text in ID and Class#into the newly created fieldexam_data$tooltip <-c(paste0( "Name = ", exam_data$ID, "\n Class = ", exam_data$CLASS)) p <-ggplot(data=exam_data, aes(x = MATHS)) +geom_dotplot_interactive(aes(tooltip = exam_data$tooltip), #newly created field used as tooltip fieldstackgroups =TRUE,binwidth =1,method ="histodot") +scale_y_continuous(NULL, breaks =NULL)#hovering over data displays student ID and Classgirafe(ggobj = p,width_svg =8,height_svg =8*0.618)
#using opts_tooltip() to customize by adding css declarations#bg of tooltip is black, font is white and boldtooltip_css <-"background-color:white; #<<font-style:bold; color:black;"#<<p <-ggplot(data=exam_data, aes(x = MATHS)) +geom_dotplot_interactive( aes(tooltip = ID), stackgroups =TRUE, binwidth =1, method ="histodot") +scale_y_continuous(NULL, breaks =NULL)girafe( ggobj = p, width_svg =6, height_svg =6*0.618,options =list( #<<opts_tooltip( #<<css = tooltip_css)) #<<)
#function used to compute 90% confidence interval of the mean#derived stats displayed in the tooltiptooltip <-function(y, ymax, accuracy = .01) { mean <- scales::number(y, accuracy = accuracy) sem <- scales::number(ymax - y, accuracy = accuracy)paste("Mean maths scores:", mean, "+/-", sem)}gg_point <-ggplot(data=exam_data, aes(x = RACE),) +stat_summary(aes(y = MATHS, tooltip =after_stat( tooltip(y, ymax))), fun.data ="mean_se", geom = GeomInteractiveCol, fill ="light blue" ) +stat_summary(aes(y = MATHS),fun.data = mean_se,geom ="errorbar", width =0.2, size =0.2 )girafe(ggobj = gg_point,width_svg =8,height_svg =8*0.618)
#TWO: data_id#elements associated with data_id(i.e. Class) will highlight upon mouse over#default value of hover css is hover_css = "fill:orange;"p <-ggplot(data=exam_data, aes(x = MATHS)) +geom_dotplot_interactive( aes(data_id = CLASS), stackgroups =TRUE, binwidth =1, method ="histodot") +scale_y_continuous(NULL, breaks =NULL)girafe( ggobj = p, width_svg =6, height_svg =6*0.618)
#combining tooltip and hover effect#data_id to be highlighted upon mouse over + tooltip will show as wellp <-ggplot(data=exam_data, aes(x = MATHS)) +geom_dotplot_interactive( aes(tooltip = CLASS, data_id = CLASS), stackgroups =TRUE, binwidth =1, method ="histodot") +scale_y_continuous(NULL, breaks =NULL)girafe( ggobj = p, width_svg =6, height_svg =6*0.618,options =list( opts_hover(css ="fill: #202020;"), opts_hover_inv(css ="opacity:0.2;") ) )
#THREE: Onclick (hotlink interactivity on the web)#web document link with a data object will be displayed on the web browser#upon mouse click#Note that click actions must be a string column in the dataset containing valid javascript instructionsexam_data$onclick <-sprintf("window.open(\"%s%s\")","https://www.moe.gov.sg/schoolfinder?journey=Primary%20school",as.character(exam_data$ID))p <-ggplot(data=exam_data, aes(x = MATHS)) +geom_dotplot_interactive( aes(onclick = onclick), stackgroups =TRUE, binwidth =1, method ="histodot") +scale_y_continuous(NULL, breaks =NULL)girafe( ggobj = p, width_svg =6, height_svg =6*0.618)
#coordinated multiple views with ggiraph#data_id aesthetic critical to link observations between plots#tooltip aesthetic is optional but good to have when mouse over a pointp1 <-ggplot(data=exam_data, aes(x = MATHS)) +geom_dotplot_interactive( aes(data_id = ID), stackgroups =TRUE, binwidth =1, method ="histodot") +coord_cartesian(xlim=c(0,100)) +scale_y_continuous(NULL, breaks =NULL)p2 <-ggplot(data=exam_data, aes(x = ENGLISH)) +geom_dotplot_interactive( aes(data_id = ID), stackgroups =TRUE, binwidth =1, method ="histodot") +coord_cartesian(xlim=c(0,100)) +scale_y_continuous(NULL, breaks =NULL)girafe(code =print(p1 + p2), width_svg =6,height_svg =3,options =list(opts_hover(css ="fill: #202020;"),opts_hover_inv(css ="opacity:0.2;") ) )
#USING PLOTLY#using plot_ly()plot_ly(data = exam_data, x =~MATHS, y =~ENGLISH)
plot_ly(data = exam_data, x =~ENGLISH, y =~MATHS, color =~RACE)
#coordinated multiple views#highlight_key is used as shared data, creates an object of class#crosstalk::SharedDatad <-highlight_key(exam_data)#ggplot2 functions to create two scatterplotsp1 <-ggplot(data=d, aes(x = MATHS,y = ENGLISH)) +geom_point(size=1) +coord_cartesian(xlim=c(0,100),ylim=c(0,100))p2 <-ggplot(data=d, aes(x = MATHS,y = SCIENCE)) +geom_point(size=1) +coord_cartesian(xlim=c(0,100),ylim=c(0,100))#subplot to place them side by sidesubplot(ggplotly(p1),ggplotly(p2))